# encoding: utf-8

import requests
from config import BaseConfig
from app.modules.common.utils import recursive_json_loads
from app.extensions import log


def beetlePost(_token_id, url, params):
    """
    请求beetle api 后台
    :param _token_id:
    :param url: 地址  例：/project/editionInfoList
    :param params: 参数列表
    {
        'key1':'value1',
        'key2':'value2',
    }
    :return:
    """
    _url = "%s%s?tokenId=%s" % (BaseConfig.BEETLE_API_URL, url, _token_id)
    for _key in params:
        _url = '%s&%s=%s' % (_url, _key, params[_key])

    _rtn = recursive_json_loads(requests.post(url = _url).content)
    log.info('============== 请求beetle,url=%s,param=%s,_rtn=%s' % (url, params, _rtn))
    return _rtn


def beetleGet(_token_id, url, params):
    """
    请求beetle api 后台
    :param _token_id:
    :param url: 地址  例：/project/editionInfoList
    :param params: 参数列表
    {
        'key1':'value1',
        'key2':'value2',
    }
    :return:
    """
    _url = "%s%s?tokenId=%s" % (BaseConfig.BEETLE_API_URL, url, _token_id)
    for _key in params:
        _url = '%s&%s=%s' % (_url, _key, params[_key])

    _rtn = recursive_json_loads(requests.get(url = _url).content)
    log.info('============== 请求beetle,url=%s,param=%s,_rtn=%s' % (url, params, _rtn))
    return _rtn
